package com.own.business.wallet.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.own.business.pay.common.constant.ConstantPay;
import com.own.business.wallet.business.service.WalletHistoryService;
import com.own.business.wallet.entity.bo.WalletHistoryBo;
import com.own.business.wallet.entity.map.WalletHistoryMapVo;
import com.own.business.wallet.entity.po.WalletHistory;
import com.own.business.wallet.entity.query.WalletHistoryQuery;
import com.own.business.wallet.entity.vo.WalletHistoryVo;
import com.own.business.wallet.mapper.WalletHistoryMapper;
import com.own.component.common.base.service.impl.AbstractBaseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * WalletHistoryServiceImpl
 *
 * @author chenxueli
 * @date 2023-07-13 16:07:39
 */
@Slf4j
@Service
public class WalletHistoryServiceImpl extends AbstractBaseService<
        WalletHistory,
        WalletHistoryBo,
        WalletHistoryVo,
        WalletHistoryMapVo,
        WalletHistoryQuery,
        WalletHistoryMapper
        > implements WalletHistoryService {

    @Resource
    private WalletHistoryMapper walletHistoryMapper;

    /**
     * 获取查询条件
     *
     * @param query 查询条件
     * @return 查询条件对象
     */
    @Override
    public LambdaQueryWrapper<WalletHistory> lambdaQueryWrapper(WalletHistoryQuery query) {
        return super.lambdaQueryWrapper(query)
                .eq(query.getUserId() != null, WalletHistory::getFkUserId, query.getUserId())
                .eq(query.getWalletId() != null, WalletHistory::getFkWalletId, query.getWalletId())
                .eq(query.getType() != null, WalletHistory::getType, query.getType())
                .eq(query.getStatus() != null, WalletHistory::getStatus, query.getStatus())
                .like(StringUtils.isNotBlank(query.getSubject()), WalletHistory::getSubject, query.getSubject())
                .like(StringUtils.isNotBlank(query.getBody()), WalletHistory::getBody, query.getBody())
                .like(StringUtils.isNotBlank(query.getTradeNo()), WalletHistory::getTradeNo, query.getTradeNo())
                .like(StringUtils.isNotBlank(query.getOutTradeNo()), WalletHistory::getOutTradeNo, query.getOutTradeNo())
                .orderByDesc(WalletHistory::getGmtCreate);
    }

    /**
     * 根据交易号获取交易记录
     *
     * @param tradeNo 交易号
     * @return 交易记录
     */
    @Override
    public WalletHistory getByTradeNo(String tradeNo) {
        var wrapper = lambdaQueryWrapper().eq(WalletHistory::getTradeNo, tradeNo);
        return getOneByWrapper(wrapper);
    }

    /**
     * 根据外部交易号获取交易记录
     *
     * @param outTradeNo 外部交易号
     * @return 交易记录
     */
    @Override
    public WalletHistory getByOutTradeNo(String outTradeNo) {
        var wrapper = lambdaQueryWrapper()
                .eq(WalletHistory::getType, ConstantPay.Type.PAY)
                .eq(WalletHistory::getOutTradeNo, outTradeNo);
        return getOneByWrapper(wrapper);
    }


    /**
     * 根据外部退款交易号获取交易记录
     *
     * @param outRefundNo 外部退款交易号
     * @return 交易记录
     */
    @Override
    public WalletHistory getByOutRefundNo(String outRefundNo) {
        var wrapper = lambdaQueryWrapper()
                .eq(WalletHistory::getType, ConstantPay.Type.REFUND)
                .eq(WalletHistory::getOutRefundNo, outRefundNo);
        return getOneByWrapper(wrapper);
    }

    /**
     * 根据外部交易号获取交易记录
     *
     * @param outTradeNo 交易号
     * @return 交易记录
     */
    @Override
    public List<WalletHistory> listByOutTradeNo(String outTradeNo) {
        var wrapper = lambdaQueryWrapper().eq(WalletHistory::getOutTradeNo, outTradeNo);
        return listByWrapper(wrapper);
    }

    /**
     * 交易关闭
     *
     * @param outTradeNo 外部交易号
     */
    @Override
    public void close(String outTradeNo) {
        var wrapper = new LambdaQueryWrapper<WalletHistory>()
                .eq(WalletHistory::getOutTradeNo, outTradeNo)
                .eq(WalletHistory::getStatus, ConstantPay.Status.NEW_BUILT);
        var history = getOneByWrapper(wrapper);
        if (history == null) {
            return;
        }
        history.setStatus(ConstantPay.Status.COMPLETE_CLOSE);
        walletHistoryMapper.updateById(history);
    }

}
